﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLVatTu.Class;

namespace QLVatTu.HeThong
{
    public partial class frmKho : Form
    {
        public frmKho()
        {
            InitializeComponent();
        }

        KetNoiDB cn = new KetNoiDB();
        string tag = "";

        private void load_button(string tag)
        {
            if (tag == "them")
            {
                btnThem.Enabled = false;
                btnSua.Enabled = false;
                btnXoa.Enabled = false;

                btnLuu.Enabled = true;
                btnHuy.Enabled = true;

                txtMaKho.Enabled = false;
                txtTenKho.Text = "";
                txtTenKho.Enabled = true;
                txtDiaChi.Text = "";
                txtDiaChi.Enabled = true;
                txtGhiChu.Text = "";
                txtGhiChu.Enabled = true;

            }
            else if (tag == "sua")
            {
                btnThem.Enabled = false;
                btnSua.Enabled = false;
                btnXoa.Enabled = false;

                btnLuu.Enabled = true;
                btnHuy.Enabled = true;

                txtMaKho.Enabled = false;
                txtTenKho.Enabled = true;
                txtDiaChi.Enabled = true;
                txtGhiChu.Enabled = true;
            }
            else
            {
                btnThem.Enabled = true;
                btnSua.Enabled = true;
                btnXoa.Enabled = true;

                btnLuu.Enabled = false;
                btnHuy.Enabled = false;

                txtMaKho.Enabled = false;
                txtTenKho.Enabled = false;
                txtDiaChi.Enabled = false;
                txtGhiChu.Enabled = false;

            }
        }

        private void load_grid()
        {
            if (cn.KetNoi())
            {
                DataTable tbl = cn.getTable("select row_number() over(order by MaKho) as STT,* from tbl_Kho");
                dtgKho.DataSource = tbl;
            }
        }
        private void frmKho_Load(object sender, EventArgs e)
        {
            load_button(tag);
           
            cn.KetNoi();

            load_grid();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            if (this.TopLevel==false) this.Parent.Parent.Controls.Remove(this.Parent);
            else this.Close();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            tag = "them";
            load_button(tag);
            txtMaKho.Text = cn.SinhMa("MaKho","tbl_Kho","KH");
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            tag = "sua";
            load_button(tag);
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (dtgKho.RowCount == 0) return;
            DialogResult msg = MessageBox.Show("Bạn có chắc chắn muốn xóa?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if(msg==DialogResult.Yes)
            {
                cn.Delete_Table("tbl_Kho","MaKho='"+txtMaKho.Text+"'");
                load_button("");
                load_grid();
            }
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (tag == "them")
            {
                if (txtTenKho.Text.ToString().Trim() == "")
                {
                    MessageBox.Show("Bạn chưa nhập tên kho.");
                    txtTenKho.Focus();
                    return;
                }
                else if (txtDiaChi.Text.ToString().Trim() == "")
                {
                    MessageBox.Show("Bạn chưa nhập địa chỉ.");
                    txtDiaChi.Focus();
                    return;
                }
                string TenKho = cn.LayDuLieu("select TenKho from tbl_Kho where lower(TenKho)=N'" + txtTenKho.Text.ToLower() + "'");
                if (TenKho != "")
                {
                    MessageBox.Show("Tên kho đã tồn tại", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtTenKho.Focus();
                    return;
                }
                cn.Insert_Table("tbl_Kho", "MaKho,TenKho,DiaChi,GhiChu", "'" + txtMaKho.Text + "',N'" + txtTenKho.Text + "',N'" + txtDiaChi.Text + "',N'" + txtGhiChu.Text + "'");

            }
            else
            {
                cn.Update_Table("tbl_Kho","TenKho=N'"+txtTenKho.Text+"',DiaChi=N'"+txtDiaChi.Text+"',GhiChu=N'"+txtGhiChu.Text+"'","MaKho='"+txtMaKho.Text+"'");
            }

            load_button("");
            load_grid();
        }

        private void dtgKho_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            load_button("");
        }

        private void dtgKho_SelectionChanged(object sender, EventArgs e)
        {
            if (dtgKho.RowCount > 0)
            {
                txtMaKho.Text = dtgKho.CurrentRow.Cells["MaKho"].Value.ToString();
                txtTenKho.Text = dtgKho.CurrentRow.Cells["TenKho"].Value.ToString();
                txtDiaChi.Text = dtgKho.CurrentRow.Cells["DiaChi"].Value.ToString();
                txtGhiChu.Text = dtgKho.CurrentRow.Cells["GhiChu"].Value.ToString();
            }
        }
    }
}
